﻿
#pragma once

#ifdef __unix__

#include "DirMonitor.h"

#include <QObject>
#include <QDebug>
#include <QString>
#include <QTimer>
#include <QDir>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/inotify.h>

class DirMonitorForLinux : public DirMonitor
{
    Q_OBJECT
public:
    explicit DirMonitorForLinux(QObject *parent = nullptr);
    virtual ~DirMonitorForLinux() override;

    virtual void SetRootDirPath(const QString &path) override;

private:
    void addDirPath(const QString &path);
    void rmDirPath(const QString &path);
    void clearDirPaths();

    void onFileClosed(int watchDesc, char *name);
    void onFileMovedOut(int watchDesc, char *name);
    void onFileMovedIn(int watchDesc, char *name);
    void onFileCreated(int watchDesc, char *name);
    void onFileDeleted(int watchDesc, char *name);
    void monitorFileEvent();
private:
    int m_fd;
    QList<QString> m_pathNeedAdd;
    QList<QString> m_pathNeedRm;
    QMap<int, QString> m_mapOfWatchDescToDirPath;
};

#endif
